<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-box">
        <h2>登录</h2>
        <el-form :model="loginForm" :rules="rules" ref="loginFormRef">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="用户名/账号" :prefix-icon="User" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              :prefix-icon="Lock"
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleLogin" :loading="loading" class="login-button">
              登录系统
            </el-button>
          </el-form-item>
          <div class="register-link">
            <el-link type="primary" @click="$router.push('/register')">注册账号</el-link>
          </div>
        </el-form>
      </div>
    </div>
  
  </div>
</template>

<script setup>
import { ref, reactive, nextTick } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'


const router = useRouter()
const userStore = useUserStore()
const loginFormRef = ref(null)
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: '',
})

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
}

const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    await loginFormRef.value.validate()
    loading.value = true

    const result = await userStore.login(loginForm.username, loginForm.password)
    console.log('Login result:', result)

    if (result && result.user) {
      ElMessage({
        message: '登录成功',
        type: 'success',
        duration: 1500,
      })

      // 确保用户状态已经更新
      await nextTick()

      console.log('Current user:', userStore.user)
      console.log('Is staff:', userStore.user?.role === 'admin' || userStore.user?.role === 'nurse')

      // 根据用户角色跳转到不同页面
      const isStaff = userStore.user?.role === 'admin' || userStore.user?.role === 'nurse'
      const targetPath = isStaff ? '/staff' : '/'

      console.log('Target path:', targetPath)

      // 使��� router.push 而不是 replace
      await router.push(targetPath)
    } else {
      console.error('No user data in response')
      ElMessage.error('登录失败：未获取到用户信息')
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.response?.data?.message || '登录失败')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.login-container {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #e6f3ff 0%, #f0f9ff 100%);
  padding: clamp(10px, 3vw, 20px);
}

.login-box {
  width: 100%;
  max-width: min(90vw, 400px);
  padding: clamp(20px, 5vw, 40px);
  background: white;
  border-radius: clamp(8px, 2vw, 12px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  color: var(--el-color-primary);
  margin-bottom: clamp(20px, 4vw, 30px);
  font-size: clamp(20px, 4vw, 24px);
  font-weight: 600;
}

.login-button {
  width: 100%;
  padding: clamp(10px, 2vw, 12px) 0;
  font-size: clamp(14px, 3vw, 16px);
  font-weight: 500;
}

.register-link {
  text-align: center;
  margin-top: clamp(12px, 3vw, 16px);
  font-size: clamp(13px, 2.5vw, 14px);
  color: var(--el-text-color-regular);
}

:deep(.el-input__wrapper) {
  padding: 0 clamp(8px, 2vw, 11px);
  box-shadow: 0 0 0 1px var(--el-border-color) inset;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px var(--el-color-primary) inset;
}

:deep(.el-input__inner) {
  height: clamp(36px, 8vw, 42px);
  font-size: clamp(14px, 3vw, 16px);
}

:deep(.el-form-item__error) {
  font-size: clamp(12px, 2.5vw, 13px);
  padding-top: 4px;
}

@media (max-width: 480px) {
  .login-box {
    margin: 10px;
    padding: 20px;
  }

  :deep(.el-form-item) {
    margin-bottom: 15px;
  }

  :deep(.el-input__inner) {
    height: 40px;
  }
}

@media (prefers-color-scheme: dark) {
  .login-container {
    background: linear-gradient(135deg, #1a1a1a 0%, #2c2c2c 100%);
  }

  .login-box {
    background: #2c2c2c;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  }

  h2 {
    color: var(--el-color-primary);
  }

  .register-link {
    color: var(--el-text-color-secondary);
  }
}
</style>
